Git Interview Questions and Answers

Git Interview Questions and Answers

1. What is Git, and why is it important?

Git is a distributed version control system that helps developers track changes, collaborate efficiently, and manage code versions.


2. What’s the difference between Git and GitHub?

Git is a version control system, while GitHub is a hosting service that provides cloud-based repositories and collaboration tools.


3. How do I revert to a previous commit in Git?

You can use:

  • git reset --hard <commit-hash> (Dangerous: overwrites history)
  • git revert <commit-hash> (Safer: creates a new commit that undoes changes)

4. How do I resolve Git merge conflicts?

Use:

  • git status to identify conflicts
  • Manually edit conflicting files
  • git add <file> and git commit to save the resolved merge

5. How can I undo the last commit?

  • git reset --soft HEAD~1 (Keep changes in staging)
  • git reset --mixed HEAD~1 (Keep changes in working directory)
  • git reset --hard HEAD~1 (Discard changes completely)

6. How do I check my Git commit history?

Run git log or git log --oneline --graph --all for a structured view.


7. How do I create and switch branches in Git?

Use:

  • git branch feature-branch (Create a new branch)
  • git checkout feature-branch or git switch feature-branch (Switch to it)

8. What is Git stash used for?

git stash temporarily saves uncommitted changes without committing them, so you can switch branches safely.


9. How do I push an existing project to GitHub?

Run:

git remote add origin <repository-url> git branch -M main git push -u origin main

10. What is the difference between git pull and git fetch?

  • git fetch downloads changes from the remote repository without applying them.
  • git pull downloads and merges the changes automatically.

11. What are the key differences between Git and SVN?

Answer:

  • Git is distributed, while SVN is centralized.
  • Git allows local commits, SVN requires a central repository.
  • Git has better branching and merging support than SVN.

12. What is the difference between git merge and git rebase?

Answer:

  • git merge creates a new merge commit and preserves history.
  • git rebase reapplies commits on top of another branch, making the history cleaner.

13. What is the HEAD in Git?

Answer:
HEAD is a pointer that represents the current commit in a branch.


14. What is the difference between git pull and git fetch?

Answer:

  • git fetch downloads changes without merging.
  • git pull fetches and merges in one step.

15. How do you resolve merge conflicts in Git?

Answer:

  1. Use git status to check conflicts.
  2. Open conflicting files and manually fix them.
  3. Add resolved files using git add <file>.
  4. Commit changes with git commit -m "Resolved merge conflict".

16. How can you undo a commit in Git?

Answer:

  • git reset --soft HEAD~1 (Undo commit, keep changes staged)
  • git reset --hard HEAD~1 (Undo commit and remove changes)
  • git revert <commit-hash> (Create a new commit that undoes changes)

17. What is Git stash, and how do you use it?

Answer:
git stash temporarily saves uncommitted changes. Use:

git stash # Save changes git stash list # View saved stashes git stash pop # Restore and remove stash

18. How do you delete a branch in Git?

Answer:

  • Locally: git branch -d <branch-name>
  • Remotely: git push origin --delete <branch-name>

19. How can you list all remote branches in Git?

Answer:
Use: git branch -r


20. What is git bisect, and how does it help debug issues?

Answer:
git bisect helps find a faulty commit using binary search:

git bisect start git bisect bad git bisect good <commit-hash>

Tags

We are Recommending you:

Leave a comment

Comments